import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;



public class Main extends JPanel implements ActionListener, KeyListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7224050476059651603L;

	Timer t;
	BananaDragon BD;
	//	Kitteh KIT;
	//	Septapus SEPT;
	List<Bunneh> BUN;



	public Main(JFrame f){

		super();
		this.setBackground(Color.WHITE);
		BD = new BananaDragon(f);
		t = new Timer(10, BD);
		this.addKeyListener(BD);
		//KIT =  new Kitteh(f);
		//	SEPT = new Septapus(f);
		BUN = new ArrayList<Bunneh>();
		BUN.add(new Bunneh(BD, 1275, 850));
		BUN.add(new Bunneh(BD, 800, 60));
		BUN.add(new Bunneh(BD, 125, 850));
		BUN.add(new Bunneh(BD, 1275, 0));




                  
		

		t.addActionListener(this);
		t.start();
	}

	public static void main(String[] args){

		JFrame jf = new JFrame("Da Aventurz ob Bunayna Drahygun");
		jf.setSize(1275, 850);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Main panel = new Main(jf);
		//jf.add(panel);
		jf.add(new Title(jf));
		jf.setVisible(true);




	}

	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D)g;
		super.paint(g);//clear screen
		BD.paint(g2d);
		//KIT.paint(g2d);
		//	SEPT.paint(g2d);

		for(Bunneh b : BUN)
		{

			b.paint(g2d);

		}

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		repaint();

		List<Bunneh> BUN2 = new ArrayList<>();

		for(Bunneh b : BUN)
		{		

			fireball f = BD.Hit(b); 
			if(f != null)
			{
				BUN2.add(b);
				BD.returnGoF().deleteBall(f);
			}

		}
		BUN.removeAll(BUN2);
	}

	@Override
	public void keyPressed(KeyEvent move) 
	{


		if(move.getKeyCode() == KeyEvent.VK_P){

			t.stop();
		}
		else 
		{
			t.start();
		}

	}



	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	
	public Bunneh Hit(Collidable c)
	{
		
		for(Bunneh f: BUN )
		{
			if(f.shape().intersects(c.shape())){
				return f;
			}
			
		}
		return null;
	}
	
	


}






